package org.opens.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @RequestMapping(value = "/login/ajax")
    public Map<String, Object> login(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password
    ) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        Map<String, Object> res = new HashMap<>();
        try {
            subject.login(token);
            res.put("code", 200);
            res.put("message", "登录成功");
            return res;
        } catch(AccountException e) {
            res.put("code", 300);
            res.put("message", e.getMessage());
            return res;
        } catch(IncorrectCredentialsException e1) {
            //这里有一个问题就是
            res.put("code", 300);
            res.put("message", "密码错误");
            return res;
        } catch(Exception e2) {
            res.put("code", 500);
            res.put("message", "服务器异常");
            return res;
        }
    }

}
